﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using PBOffice.DAL;

namespace PBOffice.BLL
{
    public class SysManageBLL
    {
        #region 查询所有的父节点
        /// <summary>
        /// 查询出所有的父节点信息
        /// </summary>
        /// <returns></returns>
        public static List<MenuItem> SelectMenu()
        {
            using (MyOfficeEntities myoffice = new MyOfficeEntities())
            {
                var fa = from v in myoffice.MenuItems
                         where v.FatherNodeNo == 0
                         select v;
                return fa.ToList();
            }
        }
        #endregion

        #region 查询节点的子节点
        /// <summary>
        /// 查询节点的的子节点
        /// </summary>
        /// <param name="MenuID">需要查询的节点</param>
        /// <returns></returns>
        public static List<MenuItem> SelectMenu(int MenuID)
        {
            using (MyOfficeEntities myoffice = new MyOfficeEntities())
            {
                var menu = from v in myoffice.MenuItems
                           where v.FatherNodeNo == MenuID
                           select v;
                return menu.ToList();
            }
        }
        #endregion

        #region 添加新的角色权限
        /// <summary>
        /// 添加新的角色权限
        /// </summary>
        /// <param name="menuitem">权限对应菜单的ID</param>
        /// <param name="roleNo">角色的编号</param>
        /// <returns></returns>
        public static bool AddPower(string menuitem, int roleNo)
        {
            using (MyOfficeEntities myoffice = new MyOfficeEntities())
            {
                var power = myoffice.sp_AddPower(menuitem, roleNo);
                if (power > 0)
                    return true;
                else
                    return false;
            }
        } 
        #endregion

        #region 查询用户的权限
        /// <summary>
        /// 查询用户的权限
        /// </summary>
        /// <param name="UserNo">用户的编号</param>
        /// <returns></returns>
        public static List<v_SelectUserRole> SelectUserPower(string RoleId)
        {
            using (MyOfficeEntities myoffice = new MyOfficeEntities())
            {
                var power = from v in myoffice.v_SelectUserRole
                            where v.RoleId==RoleId
                            select v;
                return power.ToList();
            }
        }
        #endregion

        #region 根据角色的名称查询角色的编号
        /// <summary>
        /// 根据角色的名称查询角色的编号
        /// </summary>
        /// <param name="roleId">角色的名称</param>
        /// <returns></returns>
        public static Role SelectRoleNo(int  roleNo)
        {
            using (MyOfficeEntities myoffice = new MyOfficeEntities())
            {
                var role = from v in myoffice.Roles
                           where v.RoleNo == roleNo
                           select v;
                return role.SingleOrDefault();
            }
        } 
        #endregion
    }
}
